package com.jinlejia.erp.ui.new_house.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.databinding.ItemHousetypeBinding;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author SongJiaTai
 * @description:户型列表Adapter
 * @date :2020/8/14 17:22
 */
public class HouseTypeListAdapter extends YfListAdapter<Map<String, Object>> {
    private Context mContext;

    public HouseTypeListAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_housetype, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = (TextView) view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder holder, int position) {
        ItemHousetypeBinding binding = DataBindingUtil.getBinding(holder.itemView);
        Map<String, Object> map = mData.get(position);
        if (!TextUtils.isEmpty(String.valueOf(mData.get(position).get("picUrl")))) {
            Glide.with(mContext).load(ApiConfig.MAGEHEADER_URL().concat(mData.get(position).get("picUrl").toString()))
                    .placeholder(R.mipmap.ic_no_img).into(binding.imhHousetype);
        }
        if (!TextUtils.isEmpty(String.valueOf(mData.get(position).get("houseType")))) {
            binding.tvTitle.setText(String.valueOf(mData.get(position).get("houseType")));
        }

        if (!TextUtils.isEmpty(String.valueOf(mData.get(position).get("acreage")))) {
            if (Integer.parseInt(String.valueOf(mData.get(position).get("saleState"))) == 1) {
                binding.tvMessage.setText("待售" + mData.get(position).get("acreage") + "㎡");
                binding.tvTag.setText("待售");
                binding.tvTag.setBackgroundColor(Color.parseColor("#FF8000"));
            } else {
                binding.tvMessage.setText("在售" + mData.get(position).get("acreage") + "㎡");
                binding.tvTag.setText("在售");
                binding.tvTag.setBackgroundColor(Color.parseColor("#FAA700"));
            }
        }

        if (!TextUtils.isEmpty(String.valueOf(mData.get(position).get("price"))) && !String.valueOf(mData.get(position).get("price")).equals("null")) {
            if (String.valueOf(mData.get(position).get("price")).equals("0.0")||String.valueOf(mData.get(position).get("price")).equals("0")) {
                binding.tvPrice.setText("价格待定");
            } else {
                binding.tvPrice.setText("约" + mData.get(position).get("price") + "万/套");
            }
        } else {
            binding.tvPrice.setText("价格待定");
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double id = Double.parseDouble(String.valueOf(mData.get(position).get("id")));
                int i = Integer.parseInt(new DecimalFormat("0").format(id));
                if (listener != null) {
                    listener.OnImageClick(i);
                }
            }
        });

        holder.itemView.setTag(mData.get(position));
    }

    public interface OnImageListener {
        void OnImageClick(int id);
    }

    private OnImageListener listener;

    public void OnImagesListener(OnImageListener listener) {
        this.listener = listener;
    }


    private static final class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
}
